package com.hjs.homework.state;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class StateDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(2000);
        DataStreamSource<String> source = env.socketTextStream("bogon", 7777);
        SingleOutputStreamOperator<Tuple2<Long, Long>> data = source.map(new MapFunction<String, Tuple2<Long, Long>>() {
            @Override
            public Tuple2<Long, Long> map(String s) throws Exception {
                String[] words = s.split(",");
                return new Tuple2<Long, Long>(Long.valueOf(words[0]), Long.valueOf(words[1]));
            }
        });
//        DataStreamSource<Tuple2<Long, Long>> data = env.fromElements(new Tuple2(1L, 3L), new Tuple2(1L, 5L), new Tuple2(1L, 7L), new Tuple2(1L, 4L), new Tuple2(1L, 2L));
        KeyedStream<Tuple2<Long, Long>, Long> keyByed = data.keyBy(value -> value.f0);
        SingleOutputStreamOperator<Tuple2<Long, Long>> average = keyByed.flatMap(new RichFlatMapFunction<Tuple2<Long, Long>, Tuple2<Long, Long>>() {
            ValueState<Tuple2<Long, Long>> state;

            @Override
            public void open(Configuration parameters) throws Exception {
                ValueStateDescriptor<Tuple2<Long, Long>> average = new ValueStateDescriptor<>(
                        "average",
                        TypeInformation.of(new TypeHint<Tuple2<Long, Long>>() {
                        }),
                        Tuple2.of(0L, 0L));
                state = getRuntimeContext().getState(average);
                super.open(parameters);
            }

            @Override
            public void flatMap(Tuple2<Long, Long> tuple2, Collector<Tuple2<Long, Long>> collector) throws Exception {
                Tuple2<Long, Long> currentState = state.value();
                currentState.f0 += 1;
                currentState.f1 += tuple2.f1;

                state.update(currentState);

                if (currentState.f0 == 2) {
                    collector.collect(new Tuple2<>(tuple2.f0, currentState.f1 / currentState.f0));
                    state.clear();
                }
            }
        });

        average.print();
        average.addSink(new OperatorStateDemo(2));
        env.execute();
    }
}
